package com.jie.controller;


import com.google.code.kaptcha.Producer;
import com.jie.enums.BizCodeEnum;
import com.jie.enums.SendCodeEnum;
import com.jie.service.MailService;
import com.jie.service.NotifyService;
import com.jie.utils.CommonUtil;
import com.jie.utils.JsonData;
import com.jie.vo.EmailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.sql.Time;
import java.util.concurrent.TimeUnit;

@Api(tags="通知模块")
@RestController
@RequestMapping("/api/notify_v1")
@Slf4j
public class NotifyController {
    //图片有效时间
    private static final long CREATE_CODE_EXPIRED=600;
    @Autowired
    private Producer captchProducer;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private NotifyService notifyService;

    @ApiOperation("获取图形验证码")
    @GetMapping("/captcha")
    public void getCaptcha(HttpServletRequest request, HttpServletResponse response){
        String captcha=captchProducer.createText();
        log.info("验证码:{}",captcha);
        redisTemplate.opsForValue().set(getCaptchaKey(request),captcha,CREATE_CODE_EXPIRED, TimeUnit.MINUTES);
        BufferedImage image = captchProducer.createImage(captcha);
        ServletOutputStream outputStream=null;
        try {
            outputStream=response.getOutputStream();
            ImageIO.write(image,"jpg",outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            log.info("获取验证码失败:{}",e.getMessage());
            throw new RuntimeException(e);
        }

    }
    private String getCaptchaKey(HttpServletRequest request){
        String ipAddr = CommonUtil.getIpAddr(request);
        String header = request.getHeader("User-Agent");
        String key="user:captcha:"+CommonUtil.MD5(ipAddr+header);
        log.info("key为{}",key);
        return key;
    }
    @ApiOperation("邮箱发送验证码接口")
    @PostMapping("/getEmailCaptcha")
    public JsonData getEmailCaptcha(EmailVO emailVO,HttpServletRequest request){
        //获取验证码
        String key = getCaptchaKey(request);
        //redis获取
        String cacheCaptcha = redisTemplate.opsForValue().get(key);
        if(emailVO.getCaptcha()!=null && cacheCaptcha!=null && cacheCaptcha.equalsIgnoreCase(emailVO.getCaptcha())) {
            redisTemplate.delete(key);
            JsonData jsonData = notifyService.sendCode(SendCodeEnum.USER_REGISTER,emailVO);
            return jsonData;
        }else {
            return JsonData.buildResult(BizCodeEnum.CODE_CAPTCHA);
        }
    }

}

